package com.ruoyi.tea.api;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.tea.domain.TeaUser;
import com.ruoyi.tea.service.ITeaUserService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 人员相关API
 *
 * @author wangfeng
 * @time 2022年08月04日21:17:46
 */
@Controller
@RequestMapping("/wx/api/user")
public class UserApiController {

    @Autowired
    private ITeaUserService iTeaUserService;

    @RequestMapping("/register")
    @ResponseBody
    public AjaxResult register(@RequestBody TeaUser teaUser) {
        TeaUser tmp = new TeaUser();
        tmp.setOpenId(teaUser.getOpenId());
        List<TeaUser> teaUserList = iTeaUserService.selectTeaUserList(tmp);
        if (teaUserList != null && teaUserList.size() != 0) {
            return AjaxResult.success(teaUserList.get(0));
        }
        teaUser.setUserCode(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
        iTeaUserService.insertTeaUser(teaUser);
        return AjaxResult.success(teaUser);
    }

    @RequestMapping("/info")
    @ResponseBody
    public AjaxResult info(@RequestBody TeaUser teaUser) {
        TeaUser tmp = new TeaUser();
        List<TeaUser> teaUserList = iTeaUserService.selectTeaUserList(tmp);
        if (teaUserList != null && teaUserList.size() != 0) {
            return AjaxResult.success(teaUserList.get(0));
        }
        return AjaxResult.error("用户未注册");
    }

    @RequestMapping("/update")
    @ResponseBody
    public AjaxResult update(@RequestBody TeaUser teaUser) {
        TeaUser tmp = new TeaUser();
        List<TeaUser> teaUserList = iTeaUserService.selectTeaUserList(tmp);
        if (teaUserList != null && teaUserList.size() != 0) {
            teaUser.setId(teaUserList.get(0).getId());
        }
        iTeaUserService.updateTeaUser(teaUser);
        return AjaxResult.success("用户信息已更新");
    }
}
